/*
 * EstadoHabitacionView.java
 */

package solucionessgh.Usuarios;



import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import solucionessgh.NivelUsuario.NivelUsuarioModel;
import solucionessgh.Util.ComboInfo;
import solucionessgh.Util.Reporte;

/**
 * @author roderick
 **/
public class UsuarioView2 extends javax.swing.JDialog {

    private DefaultTableModel tbl_model ;
    private int id;
    private int action;//1=nuevo,2=modificar o eliminar

    private UsuariosModel model;

    public UsuarioView2(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.setResizable(false);
        initComponents();

        model = new UsuariosModel();

        NivelUsuarioModel model_nivelusuario = new NivelUsuarioModel();
        ResultSet rs = model_nivelusuario.read();
        try {
            while ( rs.next()){
                this.cbxNivel.addItem(new ComboInfo(rs.getInt("nuscod"),rs.getString("nusdesc")));
            }
        }catch (SQLException ex) {
            System.err.println(ex.toString());
        }

        tbl_model =new DefaultTableModel(
            null,new String [] {"ID","Login","Password","Nombre","Nivel Usuario"}

        );
        this.tblLista.setModel(tbl_model);
        this.tblLista.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    int row = tblLista.getSelectedRow();
                    if(row==0){
                        btnPrimero.setEnabled(false);
                        btnAnterior.setEnabled(false);
                        btnSiguiente.setEnabled(true);
                        btnUltimo.setEnabled(true);
                    }
                    if(row==tblLista.getRowCount()-1){
                        btnSiguiente.setEnabled(false);
                        btnUltimo.setEnabled(false);
                        btnPrimero.setEnabled(true);
                        btnAnterior.setEnabled(true);
                    }
                    if(row!=0 && row!=tblLista.getRowCount()-1){
                        btnPrimero.setEnabled(true);
                        btnAnterior.setEnabled(true);
                        btnSiguiente.setEnabled(true);
                        btnUltimo.setEnabled(true);
                    }
                    if( row >= 0 ){
                        txtLogin.setEnabled(true);
                        txtPassword.setEnabled(true);
                        txtNombre.setEnabled(true);
                        cbxNivel.setEnabled(true);

                        id = Integer.parseInt(tbl_model.getValueAt( row ,0 ).toString());
                        txtLogin.setText(tbl_model.getValueAt( row ,1 ).toString());
                        txtPassword.setText(tbl_model.getValueAt( row ,2 ).toString());
                        txtNombre.setText(tbl_model.getValueAt( row ,3 ).toString());
                        int num = cbxNivel.getComponentCount();
                        for(int i = 0;i<num+2;i++){
                             ComboInfo obj = (ComboInfo) cbxNivel.getItemAt(i);
                             if(tbl_model.getValueAt( row ,4 ).toString().equals(obj.getNombre())){
                                 cbxNivel.setSelectedIndex(i);
                                 break;
                             }
                        }

                        txtLogin.setEnabled(false);
                        txtPassword.setEnabled(false);
                        txtNombre.setEnabled(false);
                        cbxNivel.setEnabled(false);
                        action=2;
                    }
                }
            });
        refresh();        
        centrarVentana();
    }

    private void centrarVentana(){
        java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
        java.awt.Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        this.setLocation(screenWidth / 4, screenHeight / 4);
    }

    private void refresh() {
        int count = tbl_model.getRowCount();
        if(count>0){
            for (int i=0;i<count;i++){
                tbl_model.removeRow(count-i-1);
            }
        }
        
        ResultSet rs = model.read();
        try {
            if(rs.next()){
                int i=0;
                tbl_model.insertRow(i, new Object[]{rs.getInt("id"),rs.getString("usulogin"), rs.getString("usupass"), rs.getString("usunom"), rs.getString("nuscod")});
                tbl_model.fireTableRowsInserted(i,i);
                i++;
                while ( rs.next()){
                    tbl_model.insertRow(i, new Object[]{rs.getInt("id"),rs.getString("usulogin"), rs.getString("usupass"), rs.getString("usunom"), rs.getString("nuscod")});
                    tbl_model.fireTableRowsInserted(i,i);
                    i++;
                }
                tblLista.setRowSelectionInterval(0, 0);
                init();
            }else{
                primera();
            }
        }catch (SQLException ex) {
                System.err.println(ex.toString());
        }
        count = tblLista.getRowCount();
        if(count<2){
            btnPrimero.setEnabled(false);
            btnAnterior.setEnabled(false);
            btnSiguiente.setEnabled(false);
            btnUltimo.setEnabled(false);
        }
    }
    
    private void primera(){

        this.btnPrimero.setEnabled(false);
        this.btnSiguiente.setEnabled(false);
        this.btnAnterior.setEnabled(false);
        this.btnUltimo.setEnabled(false);

        this.btnEliminar.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.btnImprimir.setEnabled(false);
        this.btnGrabar.setEnabled(false);
        this.btnCancelar.setEnabled(false);

        this.txtLogin.setText("");
        this.txtPassword.setText("");
        this.txtNombre.setText("");

        txtLogin.setEnabled(false);
        txtPassword.setEnabled(false);
        txtNombre.setEnabled(false);
        cbxNivel.setEnabled(false);
    }

    private void nuevo(){

        this.action=1;
        this.btnGrabar.setText("Grabar");
        this.btnGrabar.setEnabled(true);
        txtLogin.setEnabled(true);
                        txtPassword.setEnabled(true);
                        txtNombre.setEnabled(true);
                        cbxNivel.setEnabled(true);
        this.btnNuevo.setEnabled(false);


        this.btnPrimero.setEnabled(false);
        this.btnSiguiente.setEnabled(false);
        this.btnAnterior.setEnabled(false);
        this.btnUltimo.setEnabled(false);

        this.btnEliminar.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.btnImprimir.setEnabled(false);

        txtLogin.requestFocusInWindow();
        this.btnCancelar.setEnabled(true);

    }

    private void modificar_eleiminar(){
        this.action=2;       
        this.btnGrabar.setEnabled(true);
        this.btnEliminar.setEnabled(true);

        this.btnPrimero.setEnabled(false);
        this.btnSiguiente.setEnabled(false);
        this.btnAnterior.setEnabled(false);
        this.btnUltimo.setEnabled(false);

        this.btnImprimir.setEnabled(false);
        this.btnNuevo.setEnabled(false);
        this.btnEditar.setEnabled(false);
        this.btnEliminar.setEnabled(false);

        txtLogin.setEnabled(true);
                        txtPassword.setEnabled(true);
                        txtNombre.setEnabled(true);
                        cbxNivel.setEnabled(true);
        txtLogin.requestFocusInWindow();

        this.btnCancelar.setEnabled(true);


    }

    private void init(){
        this.txtLogin.setText("");
        this.txtPassword.setText("");
        this.txtNombre.setText("");

                txtLogin.setEnabled(false);
        txtPassword.setEnabled(false);
        txtNombre.setEnabled(false);
        cbxNivel.setEnabled(false);

        this.btnNuevo.setEnabled(true);
        this.btnEditar.setEnabled(true);
        this.btnImprimir.setEnabled(true);
        this.btnEliminar.setEnabled(true);

        this.btnPrimero.setEnabled(true);
        this.btnSiguiente.setEnabled(true);
        this.btnAnterior.setEnabled(true);
        this.btnUltimo.setEnabled(true);

        this.btnGrabar.setEnabled(false);
        
        this.btnCancelar.setEnabled(false);

        int row = this.tblLista.getSelectedRow();

        id = Integer.parseInt(tbl_model.getValueAt( row ,0 ).toString());
                        txtLogin.setText(tbl_model.getValueAt( row ,1 ).toString());
                        txtPassword.setText(tbl_model.getValueAt( row ,2 ).toString());
                        txtNombre.setText(tbl_model.getValueAt( row ,3 ).toString());
                        int num = cbxNivel.getComponentCount();
                        for(int i = 0;i<num+2;i++){
                             ComboInfo obj = (ComboInfo) cbxNivel.getItemAt(i);
                             if(tbl_model.getValueAt( row ,4 ).toString().equals(obj.getNombre())){
                                 cbxNivel.setSelectedIndex(i);
                                 break;
                             }
                        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblLista = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        btnPrimero = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        btnSiguiente = new javax.swing.JButton();
        btnUltimo = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnGrabar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtLogin = new javax.swing.JTextField();
        txtPassword = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        cbxNivel = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Actualizacion Niveles de Usuario");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Usuarios");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(" Lista "));

        jScrollPane1.setViewportView(tblLista);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btnPrimero.setText("|<");
        btnPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeroActionPerformed(evt);
            }
        });

        btnAnterior.setText("<");
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        btnSiguiente.setText(">");
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnUltimo.setText(">|");
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(btnPrimero, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAnterior, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSiguiente, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnUltimo, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnPrimero)
                .addComponent(btnAnterior)
                .addComponent(btnSiguiente)
                .addComponent(btnUltimo))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnGrabar.setText("Grabar");
        btnGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGrabarActionPerformed(evt);
            }
        });

        btnImprimir.setText("Imprimir");
        btnImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
            .addComponent(btnImprimir, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
            .addComponent(btnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
            .addComponent(btnGrabar, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
            .addComponent(btnEditar, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
            .addComponent(btnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
            .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(btnNuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGrabar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnImprimir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 146, Short.MAX_VALUE)
                .addComponent(btnSalir))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        jLabel2.setText("Login :");

        jLabel3.setText("Password :");

        jLabel4.setText("Nombre :");

        jLabel5.setText("Nivel Usuario :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(7, 7, 7)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                            .addComponent(txtLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                            .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(cbxNivel, 0, 183, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cbxNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeroActionPerformed
        this.tblLista.setRowSelectionInterval(0, 0);
        this.action=2;
    }//GEN-LAST:event_btnPrimeroActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        int pos = this.tblLista.getSelectedRow();
        if(pos==0)
            return;
        this.tblLista.setRowSelectionInterval(pos-1, pos-1);
        this.action=2;
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        int pos = this.tblLista.getSelectedRow();
        if(pos==this.tblLista.getRowCount()-1)
            return;
        this.tblLista.setRowSelectionInterval(pos+1, pos+1);
        this.action=2;
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        this.tblLista.setRowSelectionInterval(this.tblLista.getRowCount()-1, this.tblLista.getRowCount()-1);
        this.action=2;
    }//GEN-LAST:event_btnUltimoActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        nuevo();        
        id = 0;
        txtLogin.setText("");
        txtPassword.setText("");
        txtNombre.setText("");
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
        this.model.setUsupass(txtPassword.getText());
        this.model.setUsulogin(txtLogin.getText());
        this.model.setUsunom(txtNombre.getText());
        ComboInfo obj = (ComboInfo) this.cbxNivel.getSelectedItem();
        this.model.setNuscod(obj.getId());
        this.model.setId(id);
        if(this.action==1){//grabar            
            if(model.create()){
                JOptionPane.showMessageDialog(null,
                    "Grabacion OK",
                    "Estado Habitacion",
                    JOptionPane.INFORMATION_MESSAGE);

                txtLogin.setText("");
        txtPassword.setText("");
        txtNombre.setText("");
                refresh();
                return;
            }
        }
        if(this.action==2){//modificar
            int n = JOptionPane.showConfirmDialog(
                null,
                "Desea Mofiicar",
                "Modificar",
                JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                if(model.update()){
                    if (n == JOptionPane.YES_OPTION) {
                        JOptionPane.showMessageDialog(null,
                            "Modificacion OK",
                            "Estado Habitacion",
                            JOptionPane.INFORMATION_MESSAGE);
                        txtLogin.setText("");
        txtPassword.setText("");
        txtNombre.setText("");
                        refresh();
                    }
                }
            }
        }
    }//GEN-LAST:event_btnGrabarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        int n = JOptionPane.showConfirmDialog(
                null,
                "Desea Eliminar",
                "Eliminar",
                JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
            model.setId(this.id);
            model.delete();
            txtLogin.setText("");
        txtPassword.setText("");
        txtNombre.setText("");
            this.id=0;
            refresh();
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirActionPerformed
        Reporte rep = new Reporte("/TipoCambio/ReporteTipoCambio.jasper");
        try {
            rep.setVisible();
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_btnImprimirActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        modificar_eleiminar();
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        refresh();
        this.btnNuevo.setEnabled(true);
        //primera();//si no hay nada
        //init();
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGrabar;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnPrimero;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JButton btnUltimo;
    private javax.swing.JComboBox cbxNivel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblLista;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtPassword;
    // End of variables declaration//GEN-END:variables

}
